package com.ly.controller;

import com.ly.pojo.User;
import com.ly.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查找指定单个用户
     *
     * @return
     */
    @RequestMapping("seleteUser")
    public User hello() {
        return this.userService.queryById(2l);
    }

    /**
     * 查询所有的用户
     *
     * @return
     */
    @RequestMapping("seleteUserList")
    public List<User> userList() {
        return this.userService.userList();
    }

    /**
     * 删除指定单个用户
     *
     * @return
     */
    @RequestMapping("deleteUser")
    public int delete() {
        return this.userService.deleteById("2");
    }

    /**
     * 增加一个指定用户
     *
     * @return
     */
    @RequestMapping("insertUser")
    public int insert() {
        User user = new User();
        user.setId("100");
        user.setAge("18");
        user.setName("小红");
        return this.userService.insert(user);
    }

    /**
     * 更新指定用户
     *
     * @return
     */
    @RequestMapping("updateUser")
    public int update() {
        User user = this.userService.queryById(100l);
        if (user == null) {
            return 500;//错误码
        }
        user.setName("小丽");
        user.setAge("19");
        return this.userService.update(user);
    }
}
